﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using ALDIOrganizer;

namespace Generator
{
    public class Report
    {
        internal static DateTime Started;
        internal DateTime Completed;
        internal TimeSpan TimeTaken
        {
            get
            {
                return Completed - Started;
            }
        }

        internal RotaGenerationOutcome Outcome;
        internal bool[] FailedDays;
        

        internal KeyFlow KeyCycle;
        internal Flaws AllFlaws;

        Dictionary<Int64, double> TranslateWithAliases(Dictionary<int, double> pInput)
        {
            var result = new Dictionary<Int64, double>();
            foreach (var entry in pInput)
            {
                result.Add(
                    Aliases[entry.Key],
                    entry.Value);
            }
            return result;
        }
        internal Dictionary<Int64, double> StoreRunnerLimitBreakers
        {
            get
            {
                
                return TranslateWithAliases(AllFlaws.StoreRunners);
            }
        }
        internal Dictionary<Int64, double> StoreAssistantLimitBreakers
        {
            get
            {
                
                return TranslateWithAliases(AllFlaws.StoreAssistants);
            }
        }
        
        readonly Dictionary<Int64, KeyValuePair<double, int>> Overtimes;

        internal IEnumerable<KeyValuePair<Int64, KeyValuePair<double, int>>> GetSortedOvertimes
        {
            get
            {
                if (Overtimes.Count == 0)
                {
                    return null;
                }
                var _query =  from _entry in Overtimes                                                            
                              select new
                              {
                                  Entry = _entry,
                                  Overtime = _entry.Value.Key - _entry.Value.Value
                              };
                var _sorted = from _element in _query
                              where _element.Overtime > 0
                              orderby _element.Overtime descending
                              select _element.Entry;                                                          
                              
                return _sorted.AsEnumerable();
            }
        }

        readonly Dictionary<int, Int64> Aliases;
        
        public Report()
        {
            KeyCycle = new KeyFlow();
            AllFlaws = new Flaws();
            Aliases = new Dictionary<int, long>();
            Overtimes = new Dictionary<long, KeyValuePair<double, int>>();            
        }

        public static Report GetFailedReport(bool[] pWhatDays)
        {
            var _report = new Report {
                FailedDays = pWhatDays,
                Outcome = RotaGenerationOutcome.Failed
            };

            return _report;
        }

        public void AddAlias(int pTemporaryID, Int64 pTrueID)
        {
            if (Aliases.Keys.Contains(pTemporaryID))
            {
                return;
            }
            Aliases.Add(
                pTemporaryID,
                pTrueID);
        }

        public void AddOvertime(Int64 pID, double pWorkedHours, int pContractHours)
        {
            var _value = new KeyValuePair<double, int>(pWorkedHours, pContractHours);
            Overtimes.Add(pID, _value);
        }

        public BindingList<KeyValuePair<string, string>> AsDataSource()
        {
            var result = new BindingList<KeyValuePair<string, string>>();
            
            for (var day = 0; day < 7; day++)
            {
                var previousDay = day > 0 ?
                    day - 1 :
                    6;                

                var dayName = Time.NameOfDay(day);
                string connection;
                var link = KeyCycle.Days[day].FindCommon(KeyCycle.Days[previousDay]);
                if (link == -1)
                {
                    connection = "no connection";
                }
                else
                {
                    var name = SQLiteAccess.GetEmployeeFirstName(Aliases[link]);
                    connection = name + " brings key";

                    if (result.Count > 0)
                    {
                        var last = result.Count - 1;
                        result[last] =
                            new KeyValuePair<string, string>(result[last].Key,
                                result[last].Value
                                + "\n"
                                + name
                                + " takes key");
                    }
                }
                result.Add(new KeyValuePair<string, string>(dayName, connection));
            }
            return result;
        }


    }
}
